package com.eling.elcms.travel.service.impl;

import com.eling.elcms.basedoc.dao.IPersonalInfoDao;
import com.eling.elcms.basedoc.model.PersonalInfo;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.travel.dao.IRoomPersonalInfoDao;
import com.eling.elcms.travel.dao.IRoomRegisterDao;
import com.eling.elcms.travel.model.RoomPersonalInfo;
import com.eling.elcms.travel.model.RoomRegister;
import com.eling.elcms.travel.service.IRoomPersonalInfoManager;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoomPersonalInfoManagerImpl extends GenericManagerImpl<RoomPersonalInfo, Long> implements IRoomPersonalInfoManager{

	IRoomPersonalInfoDao roomPersonDao;
	
	@Autowired
	private IRoomRegisterDao roomRegisterDao;

	@Autowired
	private IPersonalInfoDao personInfoDao;
	@Autowired
	public void setDao(IRoomPersonalInfoDao dao) {
        this.dao = dao;
        this.roomPersonDao = dao;
    }

	@Override
	public RoomPersonalInfo saveAppointment(RoomPersonalInfo cond,Boolean waterElectric,Boolean breakfast,Double checkInPrice) {
		RoomPersonalInfo roomPersonalInfo = get(cond.getPkRoomPersonalInfo());
		Long roomRegisterPk = roomRegisterDao.queryPk(roomPersonalInfo.getPkRoomPersonalInfo());
		RoomRegister roomRegister = roomRegisterDao.get(roomRegisterPk);
		roomRegister.setBreakfast(breakfast);
		roomRegister.setWaterElectric(waterElectric);
		roomRegister.setCheckInPrice(checkInPrice);
		roomPersonalInfo.setRoomRegister(roomRegister);
		roomPersonalInfo.setCheckIn(true);
		return save(roomPersonalInfo);
	}

	@Override
	public RoomPersonalInfo saveCancel(RoomPersonalInfo cond,Boolean waterElectric,Boolean breakfast) {
		RoomPersonalInfo roomPersonalInfo = get(cond.getPkRoomPersonalInfo());
		Long roomRegisterPk = roomRegisterDao.queryPk(roomPersonalInfo.getPkRoomPersonalInfo());
		RoomRegister roomRegister = roomRegisterDao.get(roomRegisterPk);
		roomRegister.setBreakfast(breakfast);
		roomRegister.setWaterElectric(waterElectric);
		roomPersonalInfo.setRoomRegister(roomRegister);
		roomPersonalInfo.setCheckIn(false);
		return save(roomPersonalInfo);
	}

	@Override
	public PersonalInfo queryById(String idNumber) {
		
		PersonalInfo personalInfo = new PersonalInfo();
		if(idNumber != null ){
			personalInfo.setIdNumber(idNumber);
			List<PersonalInfo> list = personInfoDao.query(personalInfo);
			if(list != null && !list.isEmpty()){
				personalInfo = list.get(0);
				return personalInfo;
			}else {
				return null;
			}
		}
		return null;
	}

}
